﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickGraph;
using ComplexNetCentralityLib;
using GraphMLComplexType;
using EpidemicSimulator.states;
using EdgeWithAttributes;
namespace EpidemicSimulator.strategies
{
    public class DegreeCentralityInoculationStrategy : StateChangeStrategy
    {
        #region StateChangeStrategy Members

        public void applyStrategy(QuickGraph.UndirectedGraph<SocialNetworkNode, EdgeWithAttributes<SocialNetworkNode>> socialNetwork, QuickGraph.AdjacencyGraph<DiseaseModelNode, DiseaseModelEdge> diseaseModelGraph, int numOfUnits)
        {
            DiseaseModelNode immunizedState = null;
            foreach (DiseaseModelNode diseaseModelNode in diseaseModelGraph.Vertices)
            {
                if (diseaseModelNode.stateName == "Immunized")
                {
                    immunizedState = diseaseModelNode;
                    break;
                }
            }

            AdjacencyGraph<IGraphMLComplexType, EdgeWithAttributes.EdgeWithAttributes<IGraphMLComplexType>> network = new AdjacencyGraph<IGraphMLComplexType, EdgeWithAttributes.EdgeWithAttributes<IGraphMLComplexType>>();
            foreach (EdgeWithAttributes.EdgeWithAttributes<SocialNetworkNode> edge in socialNetwork.Edges)
            {
                EdgeWithAttributes.EdgeWithAttributes<IGraphMLComplexType> e = new EdgeWithAttributes.EdgeWithAttributes<IGraphMLComplexType>(edge.Source, edge.Target);
                network.AddVerticesAndEdge(e);
            }
            ComplexNetCentrality.degreeCentralityImportance(network);

            List<IGraphMLComplexType> list = network.Vertices.ToList();
            list.Sort(new CentralityImportanceComparer());

            foreach (IGraphMLComplexType node in list)
            {
                if (numOfUnits > 0)
                {
                    SocialNetworkNode socialNode = (SocialNetworkNode)node;
                    if (socialNode.state.stateNode.stateName == "Susceptible")
                    {
                        int maxContactsPerStep = socialNode.state.maxContacsPerStep;
                        socialNode.state = State.getStateInstance(immunizedState.stateClass);
                        socialNode.state.stateNode = immunizedState;
                        socialNode.state.diseaseModelGraph = diseaseModelGraph;
                        socialNode.state.node = socialNode;
                        socialNode.state.maxContacsPerStep = maxContactsPerStep;

                        int minContactsPerStep = socialNode.state.minContacsPerStep;
                        socialNode.state.minContacsPerStep = minContactsPerStep;
                        socialNode.newState = socialNode.state;
                        numOfUnits--;
                    }
                }
                else break;
            }
        }

        #endregion
    }
}
